//
//  ObserverDeviceListViewController.m
//  Rail
//
//  Created by hychen on 2018/3/25.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "ObserverDeviceListViewController.h"
#import "RecordTableViewCell.h"
#import "WatchVideoViewController.h"

@interface ObserverDeviceListViewController ()

@end

@implementation ObserverDeviceListViewController

- (instancetype)init {
    if (self = [super init]) {
        _status = @"";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem addSimpleTitleView:@"选择设备"];
    
    self.isOpenFooter = YES;
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshData {
    self.currentPage = 1;
    self.isOpenFooter = NO;
    BMWeakSelf
    [HttpRequest loadDeviceVideoListDataWithAreaId:self.areaId?:@"" status:self.status pageNo:[@(self.currentPage) stringValue] success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]]) {
            weakSelf.dataArray = [NSMutableArray arrayWithArray:object];
            [weakSelf.tableView reloadData];
            if (object.count > 9) {
                weakSelf.isOpenFooter = YES;
            }
            [weakSelf.tableView.mj_footer resetNoMoreData];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        Show_Msg_Error(msg)
    }];

}

- (void)loadMoreData {
    self.currentPage++;
    BMWeakSelf
    [HttpRequest loadDeviceVideoListDataWithAreaId:self.areaId?:@"" status:self.status pageNo:[@(self.currentPage) stringValue] success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]] && object.count > 0) {
            [weakSelf.dataArray addObjectsFromArray:object];
            [weakSelf.tableView.mj_footer endRefreshing];
            [weakSelf.tableView reloadData];
        }
        else {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_footer endRefreshing];
        Show_Msg_Error(msg)
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SelectVideoDeviceModel *model = [self.dataArray objectAtIndex:indexPath.row];
    RecordTableViewCell *cell = [RecordTableViewCell cellWithTableView:tableView class:[RecordTableViewCell class]];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SelectVideoDeviceModel *model = [self.dataArray objectAtIndex:indexPath.row];
    if (self.BlockCallBack) {
        self.BlockCallBack(model);
    }
    
    UIViewController *watchVc = nil;
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[WatchVideoViewController class]]) {
            watchVc = vc;
            break;
        }
    }
    if (watchVc) {
        [self.navigationController popToViewController:watchVc animated:YES];
    }
    else if (self.isPictureInPicture) {
        //
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}
@end
